#include "resource.h"
#include "stdafx.h"

#ifndef __QSAPIWINDOW_H_
#define __QSAPIWINDOW_H_

class QSAPIWindow : public CDialogImpl<QSAPIWindow>
{
public:

   public:
   enum { IDD = IDD_FORMVIEW };

   QSAPIWindow()
   {
      //This will create a blue brush 
      //could use GetSysColorBrush() instead if want to use a system brush
      m_hDialogBrush = CreateSolidBrush(RGB(255, 255, 255));

      hCursor    = ::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND));
      hOldCursor = ::LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)); 

   }
   
   ~QSAPIWindow()
   {
      ::DestroyWindow(m_hWnd);
      DeleteObject(hCursor);
      DeleteObject(hOldCursor);
      DeleteObject(m_hDialogBrush);
      DeleteObject(hHyperlinkFont);
      ::DestroyWindow(hHyperlink);
   }
   
   ///////////////////////////////////////////////////////////////////////////////////////////
   // Message Map for This Window.
   ///////////////////////////////////////////////////////////////////////////////////////////
   BEGIN_MSG_MAP(QSAPIWindow)
      MESSAGE_HANDLER(WM_CLOSE, OnClose)
      MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
      MESSAGE_HANDLER(WM_CTLCOLORDLG, OnCtlColorDlg) 
      MESSAGE_HANDLER(WM_CTLCOLORSTATIC, OnCtlColorStatic) 

      COMMAND_ID_HANDLER( IDC_BUTTON1, OnButton )
      COMMAND_HANDLER(IDC_HYPERLINKSTATIC, BN_CLICKED, OnClickedHyperlinkstatic)
      MESSAGE_HANDLER(WM_SETCURSOR, OnSetCursor)
   END_MSG_MAP()
public:
   
   LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
      DestroyWindow();
      return 0;
	}
   
   ///////////////////////////////////////////////////////////////////////////////////////////
	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		CenterWindow();
      return 0;
	}

   ///////////////////////////////////////////////////////////////////////////////////////////
   LRESULT OnButton(WORD, WORD, HWND, BOOL&)
   {
      EndDialog(m_iDoModalRet);
      
      DeleteObject(hOldCursor);
      DeleteObject(hCursor);
      DeleteObject(m_hDialogBrush);
      DeleteObject(hHyperlinkFont);
      ::DestroyWindow(hHyperlink);

      ZeroMemory(&logfont, sizeof(logfont));

      return 0;
   }

   ///////////////////////////////////////////////////////////////////////////////////////////
   LRESULT OnCtlColorDlg(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
   {
      return (long) m_hDialogBrush;
   } 

   ///////////////////////////////////////////////////////////////////////////////////////////
   LRESULT OnCtlColorStatic(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
   {
      HWND hStatic = (HWND)lParam;

      TCHAR szInfo[2048] = {0};
      
      GetObject(GetFont(), sizeof(logfont), &logfont);
      logfont.lfWeight = FW_BOLD;
      logfont.lfHeight = 14;
      //_tcscpy (logfont.lfFaceName, TEXT("Arial"));
      lstrcpy(logfont.lfFaceName, "Arial");

      
      if(hStatic == GetDlgItem(IDC_BODYSTATIC2))
      {
         logfont.lfHeight = m_iBodyFontHeight;
         logfont.lfWeight = FW_NORMAL;
         ::LoadString(_Module.GetResourceInstance(), m_iBodyText, szInfo, 2048);
         SetDlgItemText(IDC_BODYSTATIC2, szInfo);
      }// end if.
      else if(hStatic == GetDlgItem(IDC_TRADEMARK))
         {
            logfont.lfWeight = FW_NORMAL;
            logfont.lfHeight = 12;
            ::LoadString(_Module.GetResourceInstance(), IDS_TRADEMARK, szInfo, 2048);
            SetDlgItemText(IDC_TRADEMARK, szInfo);
         }// end if.
         else if(hStatic == GetDlgItem(IDC_REDBANNER))
            {
               GetObject(GetFont(), sizeof(logfont), &logfont);
               logfont.lfHeight = m_iBannerFontHeight;
               logfont.lfItalic = TRUE;
               logfont.lfWeight = FW_BOLD;
         
               SetTextColor((HDC)wParam, RGB(255,255,255));
               SetBkMode((HDC)wParam, TRANSPARENT);
         
               hHyperlinkFont = CreateFontIndirect(&logfont);
               SelectObject((HDC)wParam, hHyperlinkFont);

               ::LoadString(_Module.GetResourceInstance(), m_iBannerText, szInfo, 2048);
               SetDlgItemText(IDC_REDBANNER, szInfo);
         
               HBRUSH m_hRedBrush = CreateSolidBrush(m_clrBanner);

               return (long)m_hRedBrush;
            }// end if.
            else if(hStatic == GetDlgItem(IDC_HYPERLINKSTATIC))
               {
                  logfont.lfUnderline = TRUE;
                  SetTextColor((HDC)wParam, RGB(0, 0, 255));

                  hHyperlink = GetDlgItem(IDC_HYPERLINKSTATIC);
               }// end if.
               else if(hStatic == GetDlgItem(IDC_PRODUCTNAME))
               {
                  logfont.lfHeight = 20;
               }// end if.
      
      hHyperlinkFont = CreateFontIndirect(&logfont);
      SelectObject((HDC)wParam, hHyperlinkFont);
      SetBkMode((HDC)wParam, TRANSPARENT);

      return (long) m_hDialogBrush;
   } 
   
   ///////////////////////////////////////////////////////////////////////////////////////////
	LRESULT OnClickedHyperlinkstatic(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
      HINSTANCE h = ShellExecute(NULL, "open", "http://code.google.com/p/qsapi", NULL, NULL, SW_SHOWNORMAL);
      EndDialog(m_iDoModalRet);

      DeleteObject(hOldCursor);
      DeleteObject(hCursor);
      DeleteObject(m_hDialogBrush);
      DeleteObject(hHyperlinkFont);
      ::DestroyWindow(hHyperlink);
      
      ZeroMemory(&logfont, sizeof(logfont));

      
      return 0;
	}
   
   ///////////////////////////////////////////////////////////////////////////////////////////
	LRESULT OnSetCursor(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
      HWND hCurrent = (HWND)wParam;
      if(hCurrent == hHyperlink)
      {
         ::SetCursor(hCursor);
      }
      else
      {
         if(hOldCursor != GetCursor())
            ::SetCursor(hOldCursor);
      }
      
		return TRUE;
	}// end OnSetCursor

public:
   int      m_iBannerText;
   int      m_iBodyText;
   int      m_iBannerFontHeight;
   int      m_iBodyFontHeight;
   int      m_iDoModalRet;
   COLORREF m_clrBanner;

private:
   HBRUSH  m_hDialogBrush;
   HCURSOR hCursor, hOldCursor;
   LOGFONT logfont;
   HGDIOBJ hHyperlinkFont;
   HWND    hHyperlink;
   
   RECT    rcHyperlink;
   
};

#endif

